package com.example.riseofthedefender;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;

public class Textures 
{
	public int[] textures = new int[1];
	
	public Textures(GL10 gl, Context context, int rawID)
	{
		Bitmap bitmap;
		InputStream is = context.getResources().openRawResource(rawID);
		
		try 
		{
			BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
            bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
			bitmap = BitmapFactory.decodeStream(is, null, bitmapOptions);
		} 
		finally 
		{
			try 
			{
				is.close();
				is = null;
			} 
			catch(IOException e) 
			{
				e.printStackTrace();
			}
		}

		gl.glGenTextures(1, textures, 0);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
		bitmap.recycle();
		
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(Square.vertices.length * 4);
		byteBuffer.order(ByteOrder.nativeOrder());
		Square.vertexBuffer = byteBuffer.asFloatBuffer();
		Square.vertexBuffer.put(Square.vertices);
		Square.vertexBuffer.position(0);
		byteBuffer = ByteBuffer.allocateDirect(Square.texture.length * 4);
		byteBuffer.order(ByteOrder.nativeOrder());
		Square.textureBuffer = byteBuffer.asFloatBuffer();
		Square.textureBuffer.put(Square.texture);
		Square.textureBuffer.position(0);
	}
}